package com.maijinjie.admin.sys.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.maijinjie.common.bean.Pagination;
import com.maijinjie.common.bean.StatusBean;
import com.maijinjie.admin.sys.bean.UserBean;
import com.maijinjie.admin.sys.dao.UserMapper;
import com.maijinjie.admin.sys.field.UserFields;
import com.maijinjie.admin.sys.service.UserService;
import com.maijinjie.common.service.impl.BaseServiceImpl;
import com.maijinjie.common.util.MD5;

/**
 * UserServiceImpl
 * @author 罗正杰
 **/
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl implements UserService{
	@Resource
	UserMapper userMapper;
	
	@Override
	public UserBean getBeanById(Integer id) throws Exception{
		if(null==id || 0>=id){
			return null;
		} else {
			return (UserBean) getBeanById(userMapper, id);
		}
	}
	@Override
	public int count(Map<String, Object> params) throws Exception {
		return userMapper.count(params);
	}
	@Override
	public List<UserBean> list(Map<String, Object> params) throws Exception {
		return (List<UserBean>) userMapper.list(params);
	}
	@Override
	public StatusBean save(UserBean bean, Integer createUserId) throws Exception {
		Date currDate = new Date();
		//TODO 处理业务
		
		if(0<userMapper.save(bean)){
			return success("操作成功！");
		}
		return error("操作失败！");
	}
	@Override
	public StatusBean update(UserBean bean, Integer createUserId) throws Exception {
		if(null!=bean){
			Date currDate = new Date();
			HashMap<String, Object> params = new HashMap<String, Object>();
			//TODO 处理业务
			
			if(userMapper.update(params)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public StatusBean deleteById(Integer id) throws Exception {
		if(null!=id && 0<id){
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put(UserFields.id.toString(), id);
			if(userMapper.delete(params)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public Pagination<UserBean> list(Pagination<UserBean> pagination) throws Exception {
		return (Pagination<UserBean>) getPagination(userMapper, pagination);
	}
	@Override
	public StatusBean batchDelete(String ids) throws Exception {
		if(null!=ids && !"".equals(ids.trim())) {
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put(UserFields.id.toString(), ids);
			if(userMapper.batchDelete(params)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public List<UserBean> listAll() throws Exception {
		return (List<UserBean>) userMapper.list(null);
	}
	@Override
	public UserBean listByLogin(String userName, String password)
			throws Exception {
		return userMapper.listByLogin(userName,MD5.MD5(password));
	}
	
	public static  void main(String[] args){
		System.out.println(MD5.MD5("123456"));
	}
	@Override
	public StatusBean<?> changePassword(Integer integer, String password,
			String passwordNew, String passwordNew2) throws Exception {
		if(!passwordNew.equals(passwordNew2)){
			return error("前后密码不一致");
		}		
		UserBean user = this.getBeanById(integer);
		if(!user.getPassword().equals(MD5.MD5(password))){
			return error("原密码不正确");
		}else{
			user.setPassword(MD5.MD5(passwordNew));
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("id", user.getId());
			params.put("password", user.getPassword());
			this.userMapper.update(params);
			return success("保存成功");
		}
	}
}
